package com.zhiteng.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zhiteng.entity.ActualNode;
import com.zhiteng.entity.AdjustBill;
import com.zhiteng.entity.Assignee;
import com.zhiteng.entity.CourseNode;
import com.zhiteng.entity.CourseTable;
import com.zhiteng.entity.Procdef;
import com.zhiteng.entity.Procins;
import com.zhiteng.entity.SysDict;
import com.zhiteng.entity.Teacher;
import com.zhiteng.service.ActualNodeService;
import com.zhiteng.service.AdjustBillService;
import com.zhiteng.service.AssigneeService;
import com.zhiteng.service.CourseTableService;
import com.zhiteng.service.ProcdefService;
import com.zhiteng.service.ProcessService;
import com.zhiteng.service.SmartCampusApiService;
import com.zhiteng.service.SysDictService;
import com.zhiteng.service.TeacherService;
import com.zhiteng.utils.common.DateUtil;
import com.zhiteng.utils.common.ListPageUtil;
import com.zhiteng.utils.common.ZTResult;
import com.zhiteng.vo.ProcdefModel;
import com.zhiteng.vo.ProcinsVo;

/**
 * @author BOB
 *
 */
@RestController
@RequestMapping("/Adjust")
public class AdjustController {

	@Autowired
	private ProcessService processServiceImpl;

	@Autowired
	private CourseTableService courseTableService;

	@Autowired
	private SysDictService sysDictService;

	@Autowired
	private AdjustBillService adjustBillService;

	@Autowired
	private TeacherService TeacherService;

	@Autowired
	private ActualNodeService ActualNodeService;

	@Autowired
	private ProcdefService ProcdefServiceImpl;

	@Autowired
	private SmartCampusApiService smartCampusApiService;

	@Autowired
	private AssigneeService AssigneeServiceImpl;

	/**
	 * 保存调代课审判流程的数据
	 * 
	 * @param procdefs
	 * @return
	 */
	@RequestMapping("/saveAdjustSP")
	@ResponseBody
	public ZTResult<Void> saveAdjustSP(ProcdefModel procdefs) {
		List<Procdef> pdList = procdefs.getProcdefs();
		// 1.删除之前的流程定义
		// processService.delProcdef(pdList.get(0).getSchool_name(),pdList.get(0).getType());
		// 2.保存新的流程定义
		processServiceImpl.saveProcdefBatch(pdList);
		return ZTResult.result(0, "保存成功");
	}

	/**
	 * 查询调课原因类型
	 * 
	 * @return
	 */
	@RequestMapping("/queryAdjustDict")
	@ResponseBody
	public ZTResult<List<SysDict>> queryAdjustDict(@RequestParam("schoolId") String schoolId) {

		List<SysDict> types = sysDictService.queryAdjustDict(schoolId);
		if (types.isEmpty() || types.size() == 0) {
			return ZTResult.result(1, "失败,请先去设置调课的原因");
		}
		return ZTResult.result(0, "成功", types);
	}

	/**
	 * 保存调课的单子
	 * 
	 * @return
	 */
	@RequestMapping("/saveAdjustBill")
	@ResponseBody
	public ZTResult<List<String>> saveAdjustBill(AdjustBill adjustBill) {

		if (adjustBill.getSchool_id() == null || adjustBill.getTeacher_id() == null || adjustBill.getReason() == null
				|| adjustBill.getAdjust_date() == null || adjustBill.getOld_subject() == null
				|| adjustBill.getOld_class() == null || adjustBill.getOld_node() == null
				|| adjustBill.getType2() == null) {
			return ZTResult.result(1, "传的参数不对,或者少啦");
		}

		List<AdjustBill> AdjustBillList = adjustBillService
				.queryAdjustBillBySchoolIdAndTeacherIdAndAdjustDateAndOldSubjectAndOldClassAndOldNode(adjustBill);

		for (AdjustBill adjustBill2 : AdjustBillList) {
			if (adjustBill2.getStatus() != 2 && adjustBill.getId() == null) {
				return ZTResult.result(1, "请勿重复提交数据,审批流程已经开始或者成功");
			}
		}

		if (adjustBill.getTeacher_id() != null) {
			Teacher teacher = TeacherService.queryTeacherByTeacherId(adjustBill.getTeacher_id());
			adjustBill.setTeacher_name(teacher.getName());
		}
		if (adjustBill.getPetitioner_id() != null) {
			Teacher teacher2 = TeacherService.queryTeacherByTeacherId(adjustBill.getPetitioner_id());
			adjustBill.setPetitioner_name(teacher2.getName());
		}
		// 将日期转换为星期几,格式"2018-12-7"
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String week = DateUtil.dateToWeek2(sdf.format(adjustBill.getAdjust_date()));
		adjustBill.setWeek(week);

		CourseNode CourseNodeList = courseTableService
				.queryGradeNameBySchoolIdAndTeacherIdAndOldClassAndOldSubjectAndOld_nodeAndWeek(adjustBill);

		adjustBill.setGrade_name(CourseNodeList.getGrade_name());
		adjustBill.setStatus(0);
		adjustBill.setRevocable(0);
		if (adjustBill.getPetitioner_id() == null) {
			adjustBill.setPetitioner_class(null);
		} else {
			if (adjustBill.getType2() == 2) {// 代课
				List<CourseNode> CourseNodeListe = courseTableService
						.queryCourseNodeByTeacherId(adjustBill.getSchool_id(), adjustBill.getPetitioner_id());
				adjustBill.setPetitioner_subject(CourseNodeListe.get(0).getCourse_name());
				adjustBill.setCourse_num(adjustBill.getOld_node());
				adjustBill.setPetitioner_class(adjustBill.getOld_class());
				adjustBill.setPetitioner_gradename(adjustBill.getGrade_name());
				adjustBill.setPetitioner_date(adjustBill.getAdjust_date());
			} else {
				List<CourseNode> CourseNodeList2 = courseTableService
						.queryClassNameByTeacherNameAndKCAndRequestDayAndCourseNum(adjustBill.getSchool_id(),
								adjustBill.getPetitioner_id(), adjustBill.getPetitioner_week(),
								adjustBill.getCourse_num());
				if (CourseNodeList2.isEmpty() || CourseNodeList2.size() == 0) {
					return ZTResult.result(1, "通过学校的id和老师的id,星期,节次,查询班级名字失败");
				}
				adjustBill.setPetitioner_class(CourseNodeList2.get(0).getClass_name());
				adjustBill.setPetitioner_gradename(CourseNodeList2.get(0).getGrade_name());
				adjustBill.setPetitioner_subject(CourseNodeList2.get(0).getCourse_name());
			}
		}
//		if (adjustBill.getPetitioner_id() == null || adjustBill.getPetitioner_id().equals("")) {
//			adjustBill.setType(1);
//		} else {
//			adjustBill.setType(2);
//		}
		if (adjustBill.getType() != null) {
			adjustBill.setType(adjustBill.getType());
		} else {
			adjustBill.setType(1);
		}

		if (adjustBill.getPetitioner_id() == null || adjustBill.getPetitioner_id().equals("")) {
			adjustBill.setType(0);
		}

		if (adjustBill.getId() != null) {
			int j = adjustBillService.deleteAdjustBill(adjustBill.getId());
			if (j == 0) {
				return ZTResult.result(1, "删除调课的单子失败");
			}
		}

		adjustBill.setCreateDate(new Date());// 订单创建时间

		////////////////////////////////////////////
		if (adjustBill.getPetitioner_date() != null) {
			List<ActualNode> ActualNodeList = ActualNodeService
					.queryActualNodeBySchoolIdAndTeacherIdAndPetitionerDate(adjustBill);

			if (ActualNodeList != null && ActualNodeList.size() != 0) {
				return ZTResult.result(0, "被调课的人已经有啦,不能在进行调课");
			}
		}
		////////////////////////////////////////////////////

		int i = adjustBillService.saveAdjustBill(adjustBill);

		if (adjustBill.getPetitioner_id() == null || adjustBill.getPetitioner_id().equals("")) {
			return ZTResult.result(0, "保存调课的单子成功");
		}

		List<Procdef> ProcdefList = null;
		if (adjustBill.getType2() == 1) {// 调课
			// 根据学校的id查询审批流程定义表
//			List<Procdef> ProcdefList = processServiceImpl.queryProcdefBySchoolIdAndType(adjustBill.getSchool_id(), 2);
			ProcdefList = ProcdefServiceImpl.queryProcdefBySchoolIdAndType(adjustBill.getSchool_id(), 2);
		} else {// 代课
			ProcdefList = ProcdefServiceImpl.queryProcdefBySchoolIdAndType(adjustBill.getSchool_id(), 3);
		}

		Procins procins2 = new Procins();
		procins2.setSchool_id(adjustBill.getSchool_id());
		procins2.setType(adjustBill.getType2() + 1);
		procins2.setBill_id(adjustBill.getId());
		procins2.setTeacher_id(adjustBill.getTeacher_id());
		procins2.setPetitioner_id(adjustBill.getPetitioner_id());
		procins2.setAssignee_id(adjustBill.getPetitioner_id());
		procins2.setSequence(0);
		procins2.setStart(new Date());
		procins2.setComplated(0);

		List<Procins> procinsList = processServiceImpl.queryProcinsByBillId3(adjustBill.getId());
		if (!procinsList.isEmpty() && procinsList != null) {
			for (Procins procins : procinsList) {
				if (procins.getSequence() == 0) {
					procins2.setId(procins.getId());
					int j = processServiceImpl.updateProcinsTeacher(procins2);
					if (j == 0) {
						return ZTResult.result(1, "修改被请求人失败");
					}
				}
			}
		} else {
			int j = processServiceImpl.saveProcins(procins2);// 被请求人
			if (j != 0) {
				// 推送消息(先获取第一个审批人)
				Teacher t = TeacherService.queryTeacherById(adjustBill.getPetitioner_id());
				Map<String, String> data = new HashMap<>();
				data.put("wxuserid", t.getWxuserid());
				data.put("content", "有一条调代课请求等待你审批");
				Map<String, Object> sendMsg = smartCampusApiService.sendMsg(adjustBill.getSchool_id(), data);
				System.out.println(sendMsg);
			}

			for (Procdef procdef : ProcdefList) {
				Procins procins = new Procins();
				procins.setSchool_id(procdef.getSchool_id());
				procins.setType(procdef.getType());
				procins.setBill_id(adjustBill.getId());
				procins.setTeacher_id(adjustBill.getTeacher_id());
				procins.setPetitioner_id(adjustBill.getPetitioner_id());
				procins.setAssignee_id(procdef.getAssignee_id());
				procins.setSequence(procdef.getSequence());
//			if (procdef.getSequence() == 1) {
//				procins.setStart(new Date());
//			}
				procins.setComplated(0);
				int k = processServiceImpl.saveProcins(procins);// 审批人
			}

			if (i == 1) {
				return ZTResult.result(0, "保存调课的单子成功");
			}
			return ZTResult.result(1, "保存调课的单子失败");
		}
		return null;
	}

	/**
	 * 根据请求人的id查询调课单
	 * 
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("/queryAdjustBillByTeacherId")
	@ResponseBody
	public ZTResult<List<AdjustBill>> queryAdjustBillByTeacherId(@RequestParam("teacherId") String teacherId) {

		List<AdjustBill> AdjustBillList = adjustBillService.queryAdjustBillByTeacherId(teacherId);
		if (AdjustBillList.isEmpty() || AdjustBillList.size() == 0) {
			return ZTResult.result(1, "根据请求人的id查询调课单失败");
		}
		return ZTResult.result(0, "根据请求人的id查询调课单成功", AdjustBillList);
	}

	/**
	 * 撤回并且删除审批实例流程
	 * 
	 * @param BillId
	 * @return
	 */
	@RequestMapping("/withdrawAndDeleteProcinsByAdjustBillId")
	@ResponseBody
	public ZTResult<Void> withdrawAndDeleteProcinsByAdjustBillId(@RequestParam("billId") Long billId) {

		AdjustBill bill = adjustBillService.queryAdjustBillByBillId(billId);
		if (bill.getRevocable() == 1) {
			return ZTResult.result(1, "流程正在审批中不能撤回");
		}

		int updateStatus = adjustBillService.updateStatus(billId, -1);

		if (updateStatus == 0) {
			return ZTResult.result(1, " 撤回失败,传的billId不对");
		}

		int i = processServiceImpl.deleteProcins(billId);
		if (i == 0) {
			return ZTResult.result(1, "删除审批实例流程失败,传的billId不对");
		}

		return ZTResult.result(0, " 撤回并且删除审批实例流程成功");
	}

	/**
	 * 根据调课表的id查询审批流程
	 * 
	 * @return
	 */
	@RequestMapping("/queryProcinsByAdjustBillId")
	@ResponseBody
	public ZTResult<List<ProcinsVo>> queryProcinsByAdjustBillId(@RequestParam("BillId") Long BillId) {

		List<Procins> ProcinsList = adjustBillService.queryProcinsByAdjustBillId(BillId);
		List<ProcinsVo> list = new ArrayList<>();
		if (ProcinsList != null) {
			for (Procins procins : ProcinsList) {
				ProcinsVo vo = new ProcinsVo();
				// 通过老师的id查询老师的名字
				if (procins.getSequence() == 0) {
					Teacher teacher = TeacherService.queryTeacherById(procins.getAssignee_id());
					vo.setPetitionerName(teacher.getName());
				} else {
					Assignee assignee = AssigneeServiceImpl.queryAssigneeId(procins.getAssignee_id());
					vo.setAssigneeName(assignee.getName());
					vo.setRole(assignee.getRole());
				}
				vo.setSequence(procins.getSequence());
				vo.setStatus(procins.getStatus());
//				vo.setEnd(procins.getEnd());
				vo.setComplated(procins.getComplated());
				list.add(vo);
			}
		}
//		Map<String, Integer> hashedMap = new HashMap<>();
//		for (Procins procins : ProcinsList) {
//			hashedMap.put(procins.getAssignee_id(), procins.getStatus());
//		}
		if (ProcinsList.isEmpty() || ProcinsList.size() == 0) {
			return ZTResult.result(1, "根据调课表的id查询Procins失败");
		}
		return ZTResult.result(0, "根据调课表的id查询Procins成功", list);
	}

	/**
	 * 根据被请求人的id查询调课表
	 * 
	 * @param petitionerId
	 * @return
	 */
	@RequestMapping("/queryAdjustBillByPetitionerId")
	@ResponseBody
	public ZTResult<List<AdjustBill>> queryAdjustBillByPetitionerId(@RequestParam("petitionerId") String petitionerId) {

		List<AdjustBill> AdjustBillList = adjustBillService.queryAdjustBillByPetitionerId(petitionerId);
		if (AdjustBillList.isEmpty() || AdjustBillList.size() == 0) {
			return ZTResult.result(1, "根据被请求人的id查询调课表失败");
		}
		return ZTResult.result(0, "根据被请求人的id查询调课表成功", AdjustBillList);
	}

	/**
	 * 根据审批人的id查询调课的表单(待我确认页面)
	 * 
	 * @param assigneeId
	 * @return
	 */
	@RequestMapping("/queryAdjustBillByAssigneeId")
	@ResponseBody
	public ZTResult<List<AdjustBill>> queryAdjustBillByAssigneeId(@RequestParam("assigneeId") String assigneeId) {

		List<Procins> list = processServiceImpl.queryProcinsByAssigneeId(assigneeId);

		if (list.isEmpty() || list.size() == 0) {
			return ZTResult.result(1, "根据审批人的id查询调课的表单失败,没有要解决的请求");
		}

		List<AdjustBill> list2 = new ArrayList<>();
		for (Procins procins : list) {
			AdjustBill adjustBill = adjustBillService.queryAdjustBillByBillId(procins.getBill_id());
			list2.add(adjustBill);
		}
		return ZTResult.result(0, "根据审批人的id查询调课的表单成功", list2);
	}

	/**
	 * 根据审批人的id查询调课的表单(我已确认页面)
	 * 
	 * @param assigneeId
	 * @return
	 */
	@RequestMapping("/queryAdjustBillByAssigneeId2")
	@ResponseBody
	public ZTResult<Set<AdjustBill>> queryAdjustBillByAssigneeId2(@RequestParam("assigneeId") String assigneeId) {

		List<Procins> list = processServiceImpl.queryAdjustBillByAssigneeId2(assigneeId);
		if (list.isEmpty() || list.size() == 0) {
			return ZTResult.result(1, "根据审批人的id查询调课的表单(我已确认页面)失败,没有要解决的请求");
		}

		Set<AdjustBill> set = new HashSet<>();
		for (Procins procins : list) {
			AdjustBill adjustBill = adjustBillService.queryAdjustBillByBillId(procins.getBill_id());
			set.add(adjustBill);
		}
		return ZTResult.result(0, "根据审批人的id查询调课的表单(我已确认页面)成功", set);
	}

	/**
	 * 通过审批人的id,学校的id,调课单的id,状态,来修改状态
	 * 
	 * @param assigneeId
	 * @param schoolId
	 * @param billId
	 * @param status
	 * @return
	 */
	@RequestMapping("/updateProcinsStatus")
	@ResponseBody
	public ZTResult<Void> updateProcinsStatus(@RequestParam("assigneeId") String assigneeId,
			@RequestParam("schoolId") String schoolId, @RequestParam("billId") Long billId,
			@RequestParam("status") int status, @RequestParam("type2") int type2,
			@RequestParam("remark") String remark) {

		int i = processServiceImpl.updateProcinsStatus(schoolId, billId, status, assigneeId, remark);

		if (status == 0 && i != 0) {
			adjustBillService.updateRevocable(billId, 1);
		}

//		List<Procdef> list = processServiceImpl.queryProcdefBySchoolId(schoolId);
//		List<Procdef> list = ProcdefServiceImpl.queryProcdefBySchoolIdAndType(schoolId, type2);

		Procdef Procdef = ProcdefServiceImpl.queryProcdefBySchoolIdAndTypeAndAssigneeId(schoolId, type2, assigneeId);

//		processServiceImpl.queryProcinsBySchoolIdAndTypeAndSquence(schoolId,type2,);
		Procdef Procdef2 = null;
		if (Procdef == null) {
			Procdef2 = ProcdefServiceImpl.queryProcdefBySchoolIdAndTypeAndSquence(schoolId, type2, 0 + 1);
		} else {
			Procdef2 = ProcdefServiceImpl.queryProcdefBySchoolIdAndTypeAndSquence(schoolId, type2,
					Procdef.getSequence() + 1);
		}

		if (Procdef2 != null && status == 0) {
			List<Procins> Procins = processServiceImpl.queryProcinsBySchoolIdAndAssigneeIdAndBillId(schoolId,
					assigneeId, billId);
			for (Procins procins2 : Procins) {
				processServiceImpl.setNextStart(procins2.getSequence() + 1, schoolId, billId);

				Procins Procins2 = processServiceImpl
						.queryProcinsBySquenceAndSchoolIdAndAndBillId(procins2.getSequence() + 1, schoolId, billId);
				if (Procins2 != null) {
//					推送消息(先获取第一个审批人)
					Teacher t = TeacherService.queryTeacherById(Procins2.getAssignee_id());
					Map<String, String> data = new HashMap<>();
					data.put("wxuserid", t.getWxuserid());
					data.put("content", "有一条调代课消息需要处理");
					Map<String, Object> sendMsg = smartCampusApiService.sendMsg(schoolId, data);
					System.out.println(sendMsg);
				}
			}
		}

		if (status == 1 || Procdef2 == null && status == 0) {
			processServiceImpl.updateComplated(billId);
		}

		if (status == 1) {
			int j = adjustBillService.updateStatus(billId, 2);// 审批不通过
			if (j != 0) {

				AdjustBill bill = adjustBillService.queryAdjustBillByBillId(billId);

//				推送消息(先获取第一个审批人)
				Teacher t = TeacherService.queryTeacherById(bill.getTeacher_id());
				Map<String, String> data = new HashMap<>();
				data.put("wxuserid", t.getWxuserid());
				data.put("content", "你的" + bill.getWeek() + "-" + bill.getOld_node() + "(节次)审批不通过");
				Map<String, Object> sendMsg = smartCampusApiService.sendMsg(schoolId, data);
				System.out.println(sendMsg);

//				推送消息(先获取第一个审批人)
				Teacher t2 = TeacherService.queryTeacherById(bill.getPetitioner_id());
				Map<String, String> data2 = new HashMap<>();
				data.put("wxuserid", t2.getWxuserid());
				data.put("content", "你的第" + bill.getPetitioner_week() + "-" + bill.getCourse_num() + "(节次)审批不通过");
				Map<String, Object> sendMsg2 = smartCampusApiService.sendMsg(schoolId, data2);
				System.out.println(sendMsg2);
			}
		}
		if (Procdef2 == null && status == 0) {
			int j = adjustBillService.updateStatus(billId, 1);// 审批通过
			if (j != 0) {
				AdjustBill bill = adjustBillService.queryAdjustBillByBillId(billId);
				ActualNodeService.saveActualNode(bill);

//				推送消息(先获取第一个审批人)
				Teacher t = TeacherService.queryTeacherById(bill.getTeacher_id());
				Map<String, String> data = new HashMap<>();
				data.put("wxuserid", t.getWxuserid());
				data.put("content", "你的" + bill.getWeek() + "-" + bill.getOld_node() + "(节次)审批通过");
				Map<String, Object> sendMsg = smartCampusApiService.sendMsg(schoolId, data);
				System.out.println(sendMsg);

//				推送消息(先获取第一个审批人)
				Teacher t2 = TeacherService.queryTeacherById(bill.getPetitioner_id());
				Map<String, String> data2 = new HashMap<>();
				data.put("wxuserid", t2.getWxuserid());
				data.put("content", "你的第" + bill.getPetitioner_week() + "-" + bill.getCourse_num() + "(节次)审批通过");
				Map<String, Object> sendMsg2 = smartCampusApiService.sendMsg(schoolId, data2);
				System.out.println(sendMsg2);
			}
		}

		if (i == 0) {
			return ZTResult.result(1, "通过审批人的id,学校的id,调课单的id,状态,来修改状态失败");
		}
		return ZTResult.result(0, "通过审批人的id,学校的id,调课单的id,状态,来修改状态成功");
	}

	// **************************************************************************************************

	/**
	 * 查询后台调课页面的展示数据
	 * 
	 * @param schoolId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryAllAdjustBillBySchoolId")
	@ResponseBody
	public ZTResult<Map<String, List<AdjustBill>>> queryAllAdjustBillBySchoolId(
			@RequestParam("schoolId") String schoolId, @RequestParam("type2") Integer type2,
			@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
			@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex) throws Exception {

		List<AdjustBill> AdjustBillList = adjustBillService.queryAllAdjustBillBySchoolId(schoolId, type2);
		if (AdjustBillList.isEmpty() || AdjustBillList == null) {
			return ZTResult.result(1, " 查询后台调课页面失败,原因:查询那所学校的所有调课单失败");
		}

		List<AdjustBill> AdjustBillList2 = adjustBillService.queryAllAdjustBillBySchoolIdAndPersionIsNull(schoolId,
				type2);

		Comparator<AdjustBill> comparator = null;
		comparator = new Comparator<AdjustBill>() {
			@Override
			public int compare(AdjustBill o1, AdjustBill o2) {
				try {
					Long l = o1.getCreateDate().getTime();
					Long l2 = o2.getCreateDate().getTime();
					return l2.compareTo(l);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 0;
			}
		};

		Collections.sort(AdjustBillList, comparator);
		Collections.sort(AdjustBillList2, comparator);
		// 差集
		AdjustBillList.removeAll(AdjustBillList2);
		// 并集
		AdjustBillList2.addAll(AdjustBillList);

//		for (int i = 0; i < AdjustBillList2.size(); i++) {
//			Collections.swap(AdjustBillList2, i, AdjustBillList2.size() - i);
//		}

//		AdjustBillList.addAll(list);

		ListPageUtil<AdjustBill> listPageUtil = new ListPageUtil<AdjustBill>(AdjustBillList2, pageIndex, pageSize);
		List<AdjustBill> pagedList = listPageUtil.getPagedList();

		Map<String, List<AdjustBill>> resultMap = new HashMap<String, List<AdjustBill>>();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			for (AdjustBill tmExcpNew : pagedList) {

				if (resultMap.containsKey(sdf.format(tmExcpNew.getAdjust_date()) + "," + tmExcpNew.getTeacher_name())) {// map中异常批次已存在，将该数据存放到同一个key（key存放的是异常批次）的map中
					resultMap.get(sdf.format(tmExcpNew.getAdjust_date()) + "," + tmExcpNew.getTeacher_name())
							.add(tmExcpNew);
				} else {// map中不存在，新建key，用来存放数据
					List<AdjustBill> tmpList = new ArrayList<AdjustBill>();
					tmpList.add(tmExcpNew);
					resultMap.put(sdf.format(tmExcpNew.getAdjust_date()) + "," + tmExcpNew.getTeacher_name(), tmpList);
				}
			}
		} catch (Exception e) {
			throw new Exception("按照异常批次号对已开单数据进行分组时出现异常", e);
		}

//		return resultMap;
//		System.out.println(pagedList);

		return ZTResult.result(0, " 查询后台调课页面成功", resultMap);
	}

	/**
	 * 根据调课单的id删除,包括删除审批流程的
	 * 
	 * @param billId
	 * @return
	 */
	@RequestMapping("/deleteAdjustBill")
	@ResponseBody
	public ZTResult<Void> deleteAdjustBill(@RequestParam("billId") Long billId) {

		int i = adjustBillService.deleteAdjustBill(billId);
		if (i != 1) {
			return ZTResult.result(1, " 根据调课单的id删除失败");
		}
		processServiceImpl.deleteProcins(billId);

		return ZTResult.result(0, "删除成功");
	}

	/**
	 * 更具学校的id和班级的名字和年级名查询班级表
	 * 
	 * @param ClassName
	 * @return
	 */
	@RequestMapping("/queryCourseTableByClassName")
	@ResponseBody
	public ZTResult<List<CourseNode>> queryCourseTableByClassName(@RequestParam("ClassName") String ClassName,
			@RequestParam("schoolId") String schoolId, @RequestParam("gradeName") String gradeName) {

		List<CourseTable> courseTableList = courseTableService
				.queryCourseTableByClassNameAndSchoolNameAndGradeName(ClassName, schoolId, gradeName);
		if (courseTableList.isEmpty() || courseTableList.size() == 0) {
			return ZTResult.result(1, "更具学校的id和班级的名字和年级名查询班级表失败");
		}
		List<CourseNode> list = new ArrayList<>();
		for (CourseTable courseTable : courseTableList) {
			String node_ids = courseTable.getNode_ids();
			String[] split = node_ids.split(",");
			for (int i = 0; i < split.length; i++) {
				String CourseNodeId = split[i];
				CourseNode courseNode = courseTableService.queryCourseNodeByCourseNodeId(CourseNodeId);
				list.add(courseNode);
			}
		}
		return ZTResult.result(0, "更具学校的id和班级的名字和年级名查询班级表成功", list);
	}

	/**
	 * 更具学校的id和老师的id查询CourseTable
	 * 
	 * @param SchoolId
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("/queryCourseTableBySchoolIdAndTeacherId")
	@ResponseBody
	public ZTResult<List<CourseNode>> queryCourseTableBySchoolIdAndTeacherId(@RequestParam("SchoolId") String SchoolId,
			@RequestParam("teacherId") String teacherId) {

		List<CourseTable> courseTableList = courseTableService.queryCourseTableBySchoolIdAndTeacherId(SchoolId,
				teacherId);
		if (courseTableList.isEmpty() || courseTableList.size() == 0) {
			return ZTResult.result(1, "更具学校的id和老师的id查询CourseTable失败");
		}
		List<CourseNode> list = new ArrayList<>();
		for (CourseTable courseTable : courseTableList) {
			String node_ids = courseTable.getNode_ids();
			String[] split = node_ids.split(",");
			for (int i = 0; i < split.length; i++) {
				String CourseNodeId = split[i];
				CourseNode courseNode = courseTableService.queryCourseNodeByCourseNodeId(CourseNodeId);
				list.add(courseNode);
			}
		}
		return ZTResult.result(0, "更具学校的id和老师的id查询CourseTable成功", list);
	}

}